package com.projet.xh;

import cn.hutool.core.bean.BeanUtil;
import com.core.common.FnKit;

import java.util.ArrayList;
import java.util.List;

public class TqzzBomServer {

    /**
     * 是否需要进行序号赋值
     *
     * @return
     */
    private static boolean needFillSortNo(PrdBomVO r) {
        if (r.getProductType() == null) {
            return false;
        }
        //产品
        if (r.getProductType().startsWith("PC")) {
            return true;
        }
        //铸件
        if (r.getProductType().startsWith("MZ")) {
            return true;
        }
        //自制件，且 不是ZP装配组的需要序号
        if (r.getProductType().startsWith("PZ") || "ZP".equals(r.getMaterialGroupNo())) {
            return true;
        }
        return false;
    }

    /**
     * 查询产品的子树结构
     *
     * @param dataList
     * @return
     */
    public List<PrdBomVO> buildTree(List<PrdBomVO> dataList, String rootNo) {
        //取得根
        List<PrdBomVO> rootList = FnKit.filter(dataList, b -> FnKit.isEmpty(b.getDirectLeaderNo()) || b.getDirectLeaderNo().equals(rootNo));
        //排序
        SortBom.sortBom(rootList);
        //添加序号
        int idx = 1;
        for (PrdBomVO r : rootList) {
            r.setSortNo(null);
            List<String> list = new ArrayList<>();
            list.add(r.getProductFlag());
            list.add(r.getProductType());
            r.setMaterialTypes(list);
            if (needFillSortNo(r)) {
                if ("ZP".equals(r.getMaterialGroupNo())) {
                    r.setSortNo(null);
                    idx = buildTree2(r, dataList, "", idx);
                } else {
                    r.setSortNo(String.valueOf(idx++));
                    buildTree2(r, dataList, r.getSortNo() + ".", null);
                }

            } else {
                idx = buildTree2(r, dataList, null, null);
            }
        }
        return rootList;

    }

    /**
     * 构建树形结构
     *
     * @param parent
     * @param dataList
     * @param pre
     * @return
     */
    private static int buildTree2(PrdBomVO parent, List<PrdBomVO> dataList, String pre, Integer begin) {
        List<PrdBomVO> childList = new ArrayList<>();
        for (PrdBomVO temp : dataList) {
            if (parent.getMaterialNo().equals(temp.getDirectLeaderNo())) {
                childList.add(clone(temp));
            }
        }
        if (childList.size() == 0) {
            return 0;
        }
        //排序
        int idx = begin == null ? 1 : begin;
        childList = SortBom.sortBom(childList);
        //递归子节点
        for (PrdBomVO b : childList) {
            List<String> list = new ArrayList<>();
            list.add(b.getProductFlag());
            list.add(b.getProductType());
            b.setMaterialTypes(list);
            b.setSortNo(null);
            if (pre == null) {
                buildTree2(b, dataList, null, null);
            } else {
                //自制
                if (needFillSortNo(b)) {
                    if ("ZP".equals(b.getMaterialGroupNo())) {
                        b.setSortNo(null);
                        idx = buildTree2(b, dataList, pre, idx);
                    } else {
                        b.setSortNo(pre + (idx++));
                        buildTree2(b, dataList, b.getSortNo() + ".", null);
                    }
                } else {
                    buildTree2(b, dataList, null, null);
                }
            }
        }
        parent.setChildren(childList);
        return idx;
    }

    /**
     * 克隆
     *
     * @param data
     * @return
     */
    private static PrdBomVO clone(PrdBomVO data) {
        PrdBomVO vo = new PrdBomVO();
        BeanUtil.copyProperties(data, vo);
        vo.setChildren(null);
        return vo;
    }
}
